package com.tarena.ELSFK.day03_all.tetris;

import java.util.Random;

/**
 * 4格方块 
 */
public class Tetromino {
	protected Cell[] cells = new Cell[4];
	
	/** 随机生成 4格方块, 使用简单工厂方法模式! 
	 * randomTetromino 随机生成一个四格方块 
	 * 这个方面的返回值是多态的!
	 * */
	public static Tetromino randomTetromino(){
		Random r = new Random();
		int type = r.nextInt(7);
		switch(type){
		case 0: return new T();
		case 1: return new I();
		case 2: return new J();
		case 3: return new L();
		case 4: return new O();
		case 5: return new S();
		case 6: return new Z();
		}
		return null;
	}
	
	public Cell[] getCells() {
		return cells;
	}

	/** 下落 */
	public void softDrop(){
		for(int i=0; i<cells.length; i++){
			cells[i].moveDown();
		}
	}
	public void moveRight(){
		//System.out.println("moveRight()");
		for(int i=0; i<cells.length; i++){
			cells[i].moveRight();
		}
	}
	public void moveLeft(){
		for(int i=0; i<cells.length; i++){
			cells[i].moveLeft();
		}
	}
}
class T extends Tetromino{
	public T() {
		cells[0] = new Cell(0, 4, 0xff0000);
		cells[1] = new Cell(0, 3, 0xff0000);
		cells[2] = new Cell(0, 5, 0xff0000);
		cells[3] = new Cell(1, 4, 0xff0000);
	}
}
class I extends Tetromino{
	public I() {
		cells[0] = new Cell(0, 4, 0xffff00);
		cells[1] = new Cell(0, 3, 0xffff00);
		cells[2] = new Cell(0, 5, 0xffff00);
		cells[3] = new Cell(0, 6, 0xffff00);
	}
}
class L extends Tetromino {
	public L() {
		cells[0] = new Cell(0, 4, 0xff00ff);
		cells[1] = new Cell(0, 3, 0xff00ff);
		cells[2] = new Cell(0, 5, 0xff00ff);
		cells[3] = new Cell(1, 3, 0xff00ff);
	}
}

class J extends Tetromino {
	public J() {
		cells[0] = new Cell(0, 4, 0x00ffff);
		cells[1] = new Cell(0, 3, 0x00ffff);
		cells[2] = new Cell(0, 5, 0x00ffff);
		cells[3] = new Cell(1, 5, 0x00ffff);
	}
}

class S extends Tetromino {
	public S() {
		cells[0] = new Cell(0, 4, 0x0000ff);
		cells[1] = new Cell(0, 5, 0x0000ff);
		cells[2] = new Cell(1, 3, 0x0000ff);
		cells[3] = new Cell(1, 4, 0x0000ff);
	}
}

class Z extends Tetromino {
	public Z() {
		cells[0] = new Cell(1, 4, 0x00ff00);
		cells[1] = new Cell(0, 3, 0x00ff00);
		cells[2] = new Cell(0, 4, 0x00ff00);
		cells[3] = new Cell(1, 5, 0x00ff00);
	}
}

class O extends Tetromino {
	public O() {
		cells[0] = new Cell(0, 4, 0xffffff);
		cells[1] = new Cell(0, 5, 0xffffff);
		cells[2] = new Cell(1, 4, 0xffffff);
		cells[3] = new Cell(1, 5, 0xffffff);
	}
}











